#include <sys/types.h>          /* See NOTES */
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <net/if.h>

#include "proto.h"

int main(void)
{
	struct sockaddr_in raddr;
	struct ip_mreqn imr;

	int udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
	if (-1 == udp_socket) {
		perror("socket()");
		exit(1);
	}

	// 设置组播选项
	inet_aton(GROUP_ADDR, &imr.imr_multiaddr);
	inet_aton("0.0.0.0", &imr.imr_address);
	imr.imr_ifindex = if_nametoindex("ens33");
	if (-1 == setsockopt(udp_socket, IPPROTO_IP, IP_MULTICAST_IF, &imr, sizeof(imr))) {
		perror("setsockopt()");
		goto ERROR;
	}
	
	raddr.sin_family = AF_INET;
	inet_aton(GROUP_ADDR, &raddr.sin_addr);
	raddr.sin_port = htons(RCV_PORT);

	while (1) {
		sendto(udp_socket, "hello", 6, \
				0, (struct sockaddr *)&raddr, sizeof(raddr));
		sleep(1);
	}
	close(udp_socket);

	return 0;
ERROR:
	close(udp_socket);
	exit(1);
}




